<template>
  <div class="learning-center">

    <div class="stats-section">
      <el-row :gutter="20">
        <el-col :span="6">
          <div class="stat-card">
            <div class="stat-icon">
              <el-icon><Document /></el-icon>
            </div>
            <div class="stat-content">
              <h3>{{ learningStats.totalCourses || 0 }}</h3>
              <p>在学课程</p>
            </div>
          </div>
        </el-col>
        <el-col :span="6">
          <div class="stat-card">
            <div class="stat-icon">
              <el-icon><Clock /></el-icon>
            </div>
            <div class="stat-content">
              <h3>{{ learningStats.totalHours || 0 }}h</h3>
              <p>学习时长</p>
            </div>
          </div>
        </el-col>
        <el-col :span="6">
          <div class="stat-card">
            <div class="stat-icon">
              <el-icon><Trophy /></el-icon>
            </div>
            <div class="stat-content">
              <h3>{{ learningStats.completedCourses || 0 }}</h3>
              <p>完成课程</p>
            </div>
          </div>
        </el-col>
        <el-col :span="6">
          <div class="stat-card">
            <div class="stat-icon">
              <el-icon><Star /></el-icon>
            </div>
            <div class="stat-content">
              <h3>{{ learningStats.totalPoints || 0 }}</h3>
              <p>积分</p>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>

    <div class="content-section">
      <el-tabs v-model="activeTab" class="demo-tabs">
        <el-tab-pane label="我的课程" name="courses">
          <div class="course-list">
            <div v-if="myCourses.length === 0" class="empty-state">
              <el-icon><Document /></el-icon>
              <p>暂无在学课程</p>
              <el-button type="primary" @click="$router.push('/courses')">
                去选课
              </el-button>
            </div>
            
            <div v-else class="course-grid">
              <div v-for="course in myCourses" :key="course.id" class="course-item">
                <div class="course-image">
                  <img :src="course.cover || '/api/placeholder/300/200'" :alt="course.title" />
                  <div class="course-progress">
                    <el-progress 
                      type="circle" 
                      :percentage="course.progress || 0"
                      :size="60"
                    />
                  </div>
                </div>
                <div class="course-info">
                  <h4>{{ course.title }}</h4>
                  <p>{{ course.description }}</p>
                  <div class="course-meta">
                    <span>进度: {{ course.progress || 0 }}%</span>
                    <el-button type="primary" size="small" @click="continueLearning(course)">
                      继续学习
                    </el-button>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </el-tab-pane>
        
        <el-tab-pane label="学习日历" name="calendar">
          <div class="calendar-section">
            <el-calendar v-model="calendarDate">
              <template #date-cell="{ data }">
                <div class="calendar-day" @click="showDayDetail(data)">
                  <span>{{ data.day.split('-')[2] }}</span>
                  <div v-if="getDayStudyData(data.day)" class="study-dot"></div>
                </div>
              </template>
            </el-calendar>
          </div>
        </el-tab-pane>
        
        <el-tab-pane label="成就徽章" name="achievements">
          <div class="achievements-grid">
            <div v-for="badge in achievements" :key="badge.id" class="achievement-item">
              <div 
                class="badge-icon"
                :class="{ earned: badge.earned }"
              >
                <el-icon><Trophy /></el-icon>
              </div>
              <h4>{{ badge.name }}</h4>
              <p>{{ badge.description }}</p>
              <div v-if="!badge.earned" class="progress">
                <el-progress 
                  :percentage="badge.progress || 0"
                  :show-text="false"
                />
                <span>{{ badge.current }}/{{ badge.target }}</span>
              </div>
            </div>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useCourseStore } from '@/stores/course'
import { Document, Clock, Trophy, Star } from '@element-plus/icons-vue'

const router = useRouter()
const courseStore = useCourseStore()

const activeTab = ref('courses')
const calendarDate = ref(new Date())

const learningStats = ref({
  totalCourses: 5,
  totalHours: 24,
  completedCourses: 2,
  totalPoints: 350
})

const myCourses = ref([
  {
    id: 1,
    title: 'Vue3 高级开发',
    description: '深入学习Vue3新特性和最佳实践',
    cover: '/api/placeholder/300/200',
    progress: 75
  },
  {
    id: 2,
    title: 'TypeScript 实战',
    description: '掌握TypeScript在大型项目中的应用',
    cover: '/api/placeholder/300/200',
    progress: 45
  }
])

const achievements = ref([
  {
    id: 1,
    name: '初学者',
    description: '完成第一门课程',
    earned: true,
    current: 1,
    target: 1
  },
  {
    id: 2,
    name: '勤奋学习者',
    description: '连续学习7天',
    earned: false,
    current: 3,
    target: 7,
    progress: 43
  },
  {
    id: 3,
    name: '知识探索者',
    description: '完成5门课程',
    earned: false,
    current: 2,
    target: 5,
    progress: 40
  }
])

const continueLearning = (course) => {
  router.push(`/courses/${course.id}/learn`)
}

const getDayStudyData = (date) => {
  // 模拟数据，实际应该从API获取
  const studyDays = ['2024-01-15', '2024-01-16', '2024-01-18', '2024-01-20']
  return studyDays.includes(date)
}

const showDayDetail = (data) => {
  console.log('查看日期详情:', data)
}

onMounted(() => {
  // 加载学习数据
})
</script>

<style scoped>
.learning-center {
  padding: 20px;
  background: #f0f8ff;
  min-height: calc(100vh - 140px);
}

.stats-section {
  margin-bottom: 30px;
}

.stat-card {
  background: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  gap: 15px;
}

.stat-icon {
  width: 50px;
  height: 50px;
  background: #e1e8f0;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.stat-icon .el-icon {
  font-size: 24px;
  color: #2c5aa0;
}

.stat-content h3 {
  margin: 0 0 5px 0;
  color: #2c5aa0;
  font-size: 24px;
}

.stat-content p {
  margin: 0;
  color: #606266;
  font-size: 14px;
}

.content-section {
  background: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.course-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
}

.course-item {
  border: 1px solid #ebeef5;
  border-radius: 8px;
  overflow: hidden;
  transition: all 0.3s;
}

.course-item:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  transform: translateY(-2px);
}

.course-image {
  position: relative;
  height: 150px;
}

.course-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.course-progress {
  position: absolute;
  top: 10px;
  right: 10px;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 50%;
  padding: 2px;
}

.course-info {
  padding: 15px;
}

.course-info h4 {
  margin: 0 0 10px 0;
  color: #303133;
}

.course-info p {
  margin: 0 0 15px 0;
  color: #606266;
  font-size: 14px;
  line-height: 1.5;
}

.course-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.calendar-section {
  padding: 20px 0;
}

.calendar-day {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  position: relative;
}

.calendar-day span {
  font-size: 14px;
}

.study-dot {
  width: 6px;
  height: 6px;
  background: #2c5aa0;
  border-radius: 50%;
  position: absolute;
  bottom: 2px;
}

.achievements-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 20px;
}

.achievement-item {
  text-align: center;
  padding: 20px;
  border: 1px solid #ebeef5;
  border-radius: 8px;
  transition: all 0.3s;
}

.achievement-item:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  transform: translateY(-2px);
}

.badge-icon {
  width: 60px;
  height: 60px;
  background: #e1e8f0;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 15px;
}

.badge-icon.earned {
  background: #2c5aa0;
  color: white;
}

.achievement-item h4 {
  margin: 0 0 10px 0;
  color: #303133;
}

.achievement-item p {
  margin: 0 0 15px 0;
  color: #606266;
  font-size: 14px;
  line-height: 1.5;
}

.progress {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.progress span {
  font-size: 12px;
  color: #606266;
}

.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: #909399;
}

.empty-state .el-icon {
  margin-bottom: 16px;
  font-size: 48px;
}

@media (max-width: 768px) {
  .stat-card {
    flex-direction: column;
    text-align: center;
  }
  
  .course-grid,
  .achievements-grid {
    grid-template-columns: 1fr;
  }
}
</style>